<?php

namespace Task\Controller;

use Common\Controller\ApiController;
use Think\Log;

class TaskController extends ApiController {
    
    protected $mailobj = null;

    public function task() {
        $input = $this->request();
        $type = filter($input['type'], 'once');
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 10);
        $uid = $this->user['id'];
        $taskModel = D('Task');
        $count = $taskModel->where(['closed' => 0, 'type' => $type])->count();
        $task = $taskModel->
                field('id,title,description,sign')
                ->where(['closed' => 0, 'type' => $type])
                ->page($page, $perPage)
                ->order('id ASC')
                ->select();
        $where['user_id'] =  $uid;
        if($type=='day'){
            $time = strtotime(date('Y-m-d 00:00:00'));
            $where['time'] = ['gt',$time];
        }
        $TaskDone = D('TaskDone')->where($where)->getField('task_id',TRUE);
        foreach ($task as $k => $v) {
                $task[$k]['done'] = 0;
            if(in_array($v['id'], $TaskDone)){
                $task[$k]['done'] = 1;
            }
        }
        $pagination = pagination($count, $perPage, $page);
        $this->response($task, $pagination);
    }
  
}
